package com.jy.api.controller.clerk;

import com.jy.api.dao.StoreDao;
import com.jy.api.domain.Store;
import com.jy.api.vo.admin.store.StoreVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by lihu on 2017/11/11.
 */

@Slf4j
@RestController
@RequestMapping(value = "v1/clerk", produces = "application/json;charset=utf-8")
public class ClerkStoreController {


    @Autowired
    StoreDao storeDao;

    /**
     * 获取单店店铺信息
     *
     * @param storeId
     * @return
     */
    @RequestMapping(value = "stores/{storeId}", method = RequestMethod.GET)
    public ResponseEntity getStoreInfo(@PathVariable String storeId) {
        //数据校
        try {
            Store store = storeDao.findOne(storeId);
            if (store.getExpireTime().getTime() < new Date().getTime()) {
                return ResponseEntity.badRequest().body("店铺已到期，请续费后进行使用！");
            }
            if (store == null) {
                return new ResponseEntity("店铺信息不存在", HttpStatus.BAD_REQUEST);
            }
            StoreVo storeVos = new StoreVo();
            BeanUtils.copyProperties(store, storeVos);
            return new ResponseEntity(storeVos, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("getStoreInfo is error =>storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
